<!-- HTML header for doxygen 1.8.8-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <!-- For Mobile Devices -->
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
        <meta name="generator" content="Doxygen 1.8.17"/>
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
        <title>QuEST: test_gates.cpp Source File</title>
        <!--<link href="tabs.css" rel="stylesheet" type="text/css"/>-->
        <script type="text/javascript" src="dynsections.js"></script>
        <link href="doxygen.css" rel="stylesheet" type="text/css" />
        <link href="style.css" rel="stylesheet" type="text/css"/>
        <link href='https://fonts.googleapis.com/css?family=Roboto+Slab' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <link href="jquery.smartmenus.bootstrap.css" rel="stylesheet">
        <script type="text/javascript" src="jquery.smartmenus.js"></script>
        <!-- SmartMenus jQuery Bootstrap Addon -->
        <script type="text/javascript" src="jquery.smartmenus.bootstrap.js"></script>
        <!-- SmartMenus jQuery plugin -->
    </head>
    <body>
        <nav class="navbar navbar-default" role="navigation">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand"><img alt="Logo" src="logo.png"/></a>
                    <a class="navbar-brand"><b>QuEST</b> v3.2.0<br>The Quantum Exact Simulation Toolkit</a>
                </div>
            </div>
        </nav>
        <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
            <div class="content" id="content">
                <div class="container">
                    <div class="row">
                        <div class="col-sm-12 panel " style="padding-bottom: 15px;">
                            <div style="margin-bottom: 15px;">
<!-- end header part -->
<!-- Generated by Doxygen 1.8.17 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',false,false,'search.php','Search');
});
/* @license-end */</script>
<div id="main-nav"></div>
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d522931ffa1371640980b621734a4381.html">Users</a></li><li class="navelem"><a class="el" href="dir_70834f0dcd10de01d2e35ab402a2238e.html">tysonjones</a></li><li class="navelem"><a class="el" href="dir_1957401ef452be7528010ba0f4832a45.html">Desktop</a></li><li class="navelem"><a class="el" href="dir_dee4414d5d72b49a9429e079789ffd08.html">GithubRepos</a></li><li class="navelem"><a class="el" href="dir_a89b43d59316bb5045aa22bb53a67d29.html">QuEST</a></li><li class="navelem"><a class="el" href="dir_e791dcdec8c5d319683d04485e4067ac.html">tests</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">test_gates.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="test__gates_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160; </div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor">#include &quot;catch.hpp&quot;</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="QuEST_8h.html">QuEST.h</a>&quot;</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="utilities_8hpp.html">utilities.hpp</a>&quot;</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160; </div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment">/* allows concise use of Contains in catch&#39;s REQUIRE_THROWS_WITH */</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="keyword">using</span> Catch::Matchers::Contains;</div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160; </div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160; </div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160; </div>
<div class="line"><a name="l00015"></a><span class="lineno"><a class="line" href="group__unittest.html#ga0168f7b5f09ccbb226144053ad1b7497">   15</a></span>&#160;<a class="code" href="group__unittest.html#ga0168f7b5f09ccbb226144053ad1b7497">TEST_CASE</a>( <span class="stringliteral">&quot;collapseToOutcome&quot;</span>, <span class="stringliteral">&quot;[gates]&quot;</span> ) {</div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;    </div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;    <a class="code" href="structQureg.html">Qureg</a> vec = <a class="code" href="group__type.html#ga3392816c0643414165c2f5caeec17df0">createQureg</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;    <a class="code" href="structQureg.html">Qureg</a> mat = <a class="code" href="group__type.html#ga93e55b6650b408abb30a1d4a8bce757c">createDensityQureg</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;    </div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;    SECTION( <span class="stringliteral">&quot;correctness&quot;</span> ) {</div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;        </div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;        <span class="keywordtype">int</span> qubit = GENERATE( range(0,<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>) );</div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;        <span class="keywordtype">int</span> outcome = GENERATE( 0, 1 );</div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;        </div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;        <span class="comment">// repeat these random tests 10 times on every qubit, and for both outcomes</span></div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;        GENERATE( range(0,10) );</div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;        </div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;        SECTION( <span class="stringliteral">&quot;state-vector&quot;</span> ) {</div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;            </div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;            <span class="comment">// use a random L2 state for every qubit &amp; outcome</span></div>
<div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;            <a class="code" href="group__testutilities.html#gac9263a65c96a458b810b20baa81c5b94">QVector</a> vecRef = <a class="code" href="group__testutilities.html#gae1d2eb70b50267f8186cbd53e300d94f">getRandomStateVector</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>);</div>
<div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;            <a class="code" href="group__testutilities.html#gaf5a18d4c00dfe0523972be3bfc666362">toQureg</a>(vec, vecRef);</div>
<div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;            </div>
<div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;            <span class="comment">// calculate prob of outcome</span></div>
<div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;            <a class="code" href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a> prob = 0;</div>
<div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> ind=0; ind&lt;vecRef.size(); ind++) {</div>
<div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;                <span class="keywordtype">int</span> bit = (ind &gt;&gt; qubit) &amp; 1; <span class="comment">// target-th bit</span></div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;                <span class="keywordflow">if</span> (bit == outcome)</div>
<div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;                    prob += pow(abs(vecRef[ind]), 2);</div>
<div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;            }</div>
<div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;                </div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;            <span class="comment">// renormalise by the outcome prob</span></div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> ind=0; ind&lt;vecRef.size(); ind++) {</div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;                <span class="keywordtype">int</span> bit = (ind &gt;&gt; qubit) &amp; 1; <span class="comment">// target-th bit</span></div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;                <span class="keywordflow">if</span> (bit == outcome)</div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;                    vecRef[ind] /= sqrt(prob);</div>
<div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;                <span class="keywordflow">else</span> </div>
<div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;                    vecRef[ind] = 0;</div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;            }</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;            </div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;            <a class="code" href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a> res = <a class="code" href="group__normgate.html#ga34457f7f62ccd75fb7d3fe67cd2f815a">collapseToOutcome</a>(vec, qubit, outcome);</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;            REQUIRE( res == Approx(prob) );</div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;            REQUIRE( <a class="code" href="group__testutilities.html#ga45cc399adad2e2a67b0124851189fae5">areEqual</a>(vec, vecRef) );</div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;        }</div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;        SECTION( <span class="stringliteral">&quot;density-matrix&quot;</span> ) {</div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;            </div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;            <span class="comment">// use a random density matrix for every qubit &amp; outcome </span></div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;            <a class="code" href="group__testutilities.html#gabbc686f5f40eff22e08a2b2012ac4e41">QMatrix</a> matRef = <a class="code" href="group__testutilities.html#ga617b0fd1eb1c395762c937ad3b23e61b">getRandomDensityMatrix</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>);</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;            <a class="code" href="group__testutilities.html#gaf5a18d4c00dfe0523972be3bfc666362">toQureg</a>(mat, matRef);</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;            </div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;            <span class="comment">// prob is sum of diagonal amps (should be real) where target bit is outcome</span></div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;            <a class="code" href="group__type.html#gad863c5878a01051a2726ce94bf365428">qcomp</a> tr = 0;</div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> ind=0; ind&lt;matRef.size(); ind++) {</div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;                <span class="keywordtype">int</span> bit = (ind &gt;&gt; qubit) &amp; 1; <span class="comment">// qubit-th bit</span></div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;                <span class="keywordflow">if</span> (bit == outcome)</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;                    tr += matRef[ind][ind];</div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;            }</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;            REQUIRE( imag(tr) == Approx(0).margin(REAL_EPS) );</div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;            <a class="code" href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a> prob = real(tr);</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;            </div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;            <span class="comment">// renorm (/prob) every |*outcome*&gt;&lt;*outcome*| state, zeroing all others </span></div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> r=0; r&lt;matRef.size(); r++) {</div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;                <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> c=0; c&lt;matRef.size(); c++) {</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;                    <span class="keywordtype">int</span> ketBit = (c &gt;&gt; qubit) &amp; 1;</div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;                    <span class="keywordtype">int</span> braBit = (r &gt;&gt; qubit) &amp; 1;</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;                    </div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;                    <span class="keywordflow">if</span> (ketBit == outcome &amp;&amp; braBit == outcome)</div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;                        matRef[r][c] /= prob;</div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;                    <span class="keywordflow">else</span></div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;                        matRef[r][c] = 0;</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;                }</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;            }</div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;            </div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;            <a class="code" href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a> res = <a class="code" href="group__normgate.html#ga34457f7f62ccd75fb7d3fe67cd2f815a">collapseToOutcome</a>(mat, qubit, outcome);</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;            REQUIRE( res == Approx(prob) );</div>
<div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;            REQUIRE( <a class="code" href="group__testutilities.html#ga45cc399adad2e2a67b0124851189fae5">areEqual</a>(mat, matRef) );</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;        }</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;    }</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;    SECTION( <span class="stringliteral">&quot;input validation&quot;</span> ) {</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;        </div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;        SECTION( <span class="stringliteral">&quot;qubit index&quot;</span> ) {</div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;            </div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;            <span class="keywordtype">int</span> qubit = GENERATE( -1, <a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a> );</div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;            <span class="keywordtype">int</span> outcome = 0;</div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;            REQUIRE_THROWS_WITH( <a class="code" href="group__normgate.html#ga34457f7f62ccd75fb7d3fe67cd2f815a">collapseToOutcome</a>(mat, qubit, outcome), Contains(<span class="stringliteral">&quot;Invalid target qubit&quot;</span>) );</div>
<div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;        }</div>
<div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;        SECTION( <span class="stringliteral">&quot;outcome value&quot;</span> ) {</div>
<div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;            </div>
<div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;            <span class="keywordtype">int</span> qubit = 0;</div>
<div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;            <span class="keywordtype">int</span> outcome = GENERATE( -1, 2 );</div>
<div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;            REQUIRE_THROWS_WITH( <a class="code" href="group__normgate.html#ga34457f7f62ccd75fb7d3fe67cd2f815a">collapseToOutcome</a>(mat, qubit, outcome), Contains(<span class="stringliteral">&quot;Invalid measurement outcome&quot;</span>) );</div>
<div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;        }</div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;        SECTION( <span class="stringliteral">&quot;outcome probability&quot;</span> ) {</div>
<div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;            </div>
<div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;            <a class="code" href="group__init.html#ga2f049055d4a51d0ea4d1a43b5234dedb">initZeroState</a>(vec);</div>
<div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;            REQUIRE_THROWS_WITH( <a class="code" href="group__normgate.html#ga34457f7f62ccd75fb7d3fe67cd2f815a">collapseToOutcome</a>(vec, 0, 1), Contains(<span class="stringliteral">&quot;Can&#39;t collapse to state with zero probability&quot;</span>) );</div>
<div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;            <a class="code" href="group__init.html#ga0379e21efca0767a22607fc0e26a5a22">initClassicalState</a>(vec, 1);</div>
<div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;            REQUIRE_THROWS_WITH( <a class="code" href="group__normgate.html#ga34457f7f62ccd75fb7d3fe67cd2f815a">collapseToOutcome</a>(vec, 0, 0), Contains(<span class="stringliteral">&quot;Can&#39;t collapse to state with zero probability&quot;</span>) );</div>
<div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;        }</div>
<div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;    }</div>
<div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;    <a class="code" href="group__type.html#ga8e1e6ad3254892edc2ee08712ea9d692">destroyQureg</a>(vec, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;    <a class="code" href="group__type.html#ga8e1e6ad3254892edc2ee08712ea9d692">destroyQureg</a>(mat, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;}</div>
<div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160; </div>
<div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160; </div>
<div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160; </div>
<div class="line"><a name="l00121"></a><span class="lineno"><a class="line" href="group__unittest.html#ga1562bc2faa4f587e665f8974cbcc62f4">  121</a></span>&#160;<a class="code" href="group__unittest.html#ga0168f7b5f09ccbb226144053ad1b7497">TEST_CASE</a>( <span class="stringliteral">&quot;measure&quot;</span>, <span class="stringliteral">&quot;[gates]&quot;</span> ) {</div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;    </div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;    <a class="code" href="structQureg.html">Qureg</a> vec = <a class="code" href="group__type.html#ga3392816c0643414165c2f5caeec17df0">createQureg</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;    <a class="code" href="structQureg.html">Qureg</a> mat = <a class="code" href="group__type.html#ga93e55b6650b408abb30a1d4a8bce757c">createDensityQureg</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;        </div>
<div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;    SECTION( <span class="stringliteral">&quot;correctness&quot;</span> ) {</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;        </div>
<div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;        <span class="keywordtype">int</span> qubit = GENERATE( range(0,<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>) );</div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;        </div>
<div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;        <span class="comment">// repeat these random tests 10 times on every qubit </span></div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;        GENERATE( range(0,10) );</div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;        </div>
<div class="line"><a name="l00133"></a><span class="lineno">  133</span>&#160;        SECTION( <span class="stringliteral">&quot;state-vector&quot;</span> ) {</div>
<div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;            </div>
<div class="line"><a name="l00135"></a><span class="lineno">  135</span>&#160;            <a class="code" href="group__testutilities.html#gac9263a65c96a458b810b20baa81c5b94">QVector</a> vecRef = <a class="code" href="group__testutilities.html#gae1d2eb70b50267f8186cbd53e300d94f">getRandomStateVector</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>);</div>
<div class="line"><a name="l00136"></a><span class="lineno">  136</span>&#160;            <a class="code" href="group__testutilities.html#gaf5a18d4c00dfe0523972be3bfc666362">toQureg</a>(vec, vecRef);</div>
<div class="line"><a name="l00137"></a><span class="lineno">  137</span>&#160;            </div>
<div class="line"><a name="l00138"></a><span class="lineno">  138</span>&#160;            <span class="keywordtype">int</span> outcome = <a class="code" href="group__normgate.html#ga2a3794103125f1e3cfa103f8b1963656">measure</a>(vec, qubit);</div>
<div class="line"><a name="l00139"></a><span class="lineno">  139</span>&#160;            REQUIRE( (outcome == 0 || outcome == 1) );</div>
<div class="line"><a name="l00140"></a><span class="lineno">  140</span>&#160;            </div>
<div class="line"><a name="l00141"></a><span class="lineno">  141</span>&#160;            <span class="comment">// calculate prob of this outcome</span></div>
<div class="line"><a name="l00142"></a><span class="lineno">  142</span>&#160;            <a class="code" href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a> prob = 0;</div>
<div class="line"><a name="l00143"></a><span class="lineno">  143</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> ind=0; ind&lt;vecRef.size(); ind++) {</div>
<div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;                <span class="keywordtype">int</span> bit = (ind &gt;&gt; qubit) &amp; 1; <span class="comment">// target-th bit</span></div>
<div class="line"><a name="l00145"></a><span class="lineno">  145</span>&#160;                <span class="keywordflow">if</span> (bit == outcome)</div>
<div class="line"><a name="l00146"></a><span class="lineno">  146</span>&#160;                    prob += pow(abs(vecRef[ind]), 2);</div>
<div class="line"><a name="l00147"></a><span class="lineno">  147</span>&#160;            }</div>
<div class="line"><a name="l00148"></a><span class="lineno">  148</span>&#160;                </div>
<div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;            REQUIRE( prob &gt; REAL_EPS );</div>
<div class="line"><a name="l00150"></a><span class="lineno">  150</span>&#160;                </div>
<div class="line"><a name="l00151"></a><span class="lineno">  151</span>&#160;            <span class="comment">// renormalise by the outcome prob</span></div>
<div class="line"><a name="l00152"></a><span class="lineno">  152</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> ind=0; ind&lt;vecRef.size(); ind++) {</div>
<div class="line"><a name="l00153"></a><span class="lineno">  153</span>&#160;                <span class="keywordtype">int</span> bit = (ind &gt;&gt; qubit) &amp; 1; <span class="comment">// target-th bit</span></div>
<div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;                <span class="keywordflow">if</span> (bit == outcome)</div>
<div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160;                    vecRef[ind] /= sqrt(prob);</div>
<div class="line"><a name="l00156"></a><span class="lineno">  156</span>&#160;                <span class="keywordflow">else</span> </div>
<div class="line"><a name="l00157"></a><span class="lineno">  157</span>&#160;                    vecRef[ind] = 0;</div>
<div class="line"><a name="l00158"></a><span class="lineno">  158</span>&#160;            }</div>
<div class="line"><a name="l00159"></a><span class="lineno">  159</span>&#160;            REQUIRE( <a class="code" href="group__testutilities.html#ga45cc399adad2e2a67b0124851189fae5">areEqual</a>(vec, vecRef) );</div>
<div class="line"><a name="l00160"></a><span class="lineno">  160</span>&#160;        }</div>
<div class="line"><a name="l00161"></a><span class="lineno">  161</span>&#160;        SECTION( <span class="stringliteral">&quot;density-matrix&quot;</span> ) {</div>
<div class="line"><a name="l00162"></a><span class="lineno">  162</span>&#160;            </div>
<div class="line"><a name="l00163"></a><span class="lineno">  163</span>&#160;            <a class="code" href="group__testutilities.html#gabbc686f5f40eff22e08a2b2012ac4e41">QMatrix</a> matRef = <a class="code" href="group__testutilities.html#ga617b0fd1eb1c395762c937ad3b23e61b">getRandomDensityMatrix</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>);</div>
<div class="line"><a name="l00164"></a><span class="lineno">  164</span>&#160;            <a class="code" href="group__testutilities.html#gaf5a18d4c00dfe0523972be3bfc666362">toQureg</a>(mat, matRef);</div>
<div class="line"><a name="l00165"></a><span class="lineno">  165</span>&#160;            </div>
<div class="line"><a name="l00166"></a><span class="lineno">  166</span>&#160;            <span class="keywordtype">int</span> outcome = <a class="code" href="group__normgate.html#ga2a3794103125f1e3cfa103f8b1963656">measure</a>(mat, qubit);</div>
<div class="line"><a name="l00167"></a><span class="lineno">  167</span>&#160;            REQUIRE( (outcome == 0 || outcome == 1) );</div>
<div class="line"><a name="l00168"></a><span class="lineno">  168</span>&#160;            </div>
<div class="line"><a name="l00169"></a><span class="lineno">  169</span>&#160;            <span class="comment">// compute prob of this outcome</span></div>
<div class="line"><a name="l00170"></a><span class="lineno">  170</span>&#160;            <a class="code" href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a> prob = 0;</div>
<div class="line"><a name="l00171"></a><span class="lineno">  171</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> ind=0; ind&lt;matRef.size(); ind++) {</div>
<div class="line"><a name="l00172"></a><span class="lineno">  172</span>&#160;                <span class="keywordtype">int</span> bit = (ind &gt;&gt; qubit) &amp; 1; <span class="comment">// qubit-th bit</span></div>
<div class="line"><a name="l00173"></a><span class="lineno">  173</span>&#160;                <span class="keywordflow">if</span> (bit == outcome)</div>
<div class="line"><a name="l00174"></a><span class="lineno">  174</span>&#160;                    prob += real(matRef[ind][ind]);</div>
<div class="line"><a name="l00175"></a><span class="lineno">  175</span>&#160;            }</div>
<div class="line"><a name="l00176"></a><span class="lineno">  176</span>&#160;            </div>
<div class="line"><a name="l00177"></a><span class="lineno">  177</span>&#160;            REQUIRE( prob &gt; REAL_EPS );</div>
<div class="line"><a name="l00178"></a><span class="lineno">  178</span>&#160;                        </div>
<div class="line"><a name="l00179"></a><span class="lineno">  179</span>&#160;            <span class="comment">// renorm (/prob) every |*outcome*&gt;&lt;*outcome*| state, zeroing all others </span></div>
<div class="line"><a name="l00180"></a><span class="lineno">  180</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> r=0; r&lt;matRef.size(); r++) {</div>
<div class="line"><a name="l00181"></a><span class="lineno">  181</span>&#160;                <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> c=0; c&lt;matRef.size(); c++) {</div>
<div class="line"><a name="l00182"></a><span class="lineno">  182</span>&#160;                    <span class="keywordtype">int</span> ketBit = (c &gt;&gt; qubit) &amp; 1;</div>
<div class="line"><a name="l00183"></a><span class="lineno">  183</span>&#160;                    <span class="keywordtype">int</span> braBit = (r &gt;&gt; qubit) &amp; 1;</div>
<div class="line"><a name="l00184"></a><span class="lineno">  184</span>&#160;                    </div>
<div class="line"><a name="l00185"></a><span class="lineno">  185</span>&#160;                    <span class="keywordflow">if</span> (ketBit == outcome &amp;&amp; braBit == outcome)</div>
<div class="line"><a name="l00186"></a><span class="lineno">  186</span>&#160;                        matRef[r][c] /= prob;</div>
<div class="line"><a name="l00187"></a><span class="lineno">  187</span>&#160;                    <span class="keywordflow">else</span></div>
<div class="line"><a name="l00188"></a><span class="lineno">  188</span>&#160;                        matRef[r][c] = 0;</div>
<div class="line"><a name="l00189"></a><span class="lineno">  189</span>&#160;                }</div>
<div class="line"><a name="l00190"></a><span class="lineno">  190</span>&#160;            }</div>
<div class="line"><a name="l00191"></a><span class="lineno">  191</span>&#160;            </div>
<div class="line"><a name="l00192"></a><span class="lineno">  192</span>&#160;            REQUIRE( <a class="code" href="group__testutilities.html#ga45cc399adad2e2a67b0124851189fae5">areEqual</a>(mat, matRef) );</div>
<div class="line"><a name="l00193"></a><span class="lineno">  193</span>&#160;        }</div>
<div class="line"><a name="l00194"></a><span class="lineno">  194</span>&#160;    }</div>
<div class="line"><a name="l00195"></a><span class="lineno">  195</span>&#160;    SECTION( <span class="stringliteral">&quot;input validation&quot;</span> ) {</div>
<div class="line"><a name="l00196"></a><span class="lineno">  196</span>&#160;        </div>
<div class="line"><a name="l00197"></a><span class="lineno">  197</span>&#160;        SECTION( <span class="stringliteral">&quot;qubit index&quot;</span> ) {</div>
<div class="line"><a name="l00198"></a><span class="lineno">  198</span>&#160;            </div>
<div class="line"><a name="l00199"></a><span class="lineno">  199</span>&#160;            <span class="keywordtype">int</span> qubit = GENERATE( -1, <a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a> );</div>
<div class="line"><a name="l00200"></a><span class="lineno">  200</span>&#160;            REQUIRE_THROWS_WITH( <a class="code" href="group__normgate.html#ga2a3794103125f1e3cfa103f8b1963656">measure</a>(vec, qubit), Contains(<span class="stringliteral">&quot;Invalid target qubit&quot;</span>) );</div>
<div class="line"><a name="l00201"></a><span class="lineno">  201</span>&#160;        }</div>
<div class="line"><a name="l00202"></a><span class="lineno">  202</span>&#160;    }</div>
<div class="line"><a name="l00203"></a><span class="lineno">  203</span>&#160;    <a class="code" href="group__type.html#ga8e1e6ad3254892edc2ee08712ea9d692">destroyQureg</a>(vec, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00204"></a><span class="lineno">  204</span>&#160;    <a class="code" href="group__type.html#ga8e1e6ad3254892edc2ee08712ea9d692">destroyQureg</a>(mat, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00205"></a><span class="lineno">  205</span>&#160;}</div>
<div class="line"><a name="l00206"></a><span class="lineno">  206</span>&#160; </div>
<div class="line"><a name="l00207"></a><span class="lineno">  207</span>&#160; </div>
<div class="line"><a name="l00208"></a><span class="lineno">  208</span>&#160; </div>
<div class="line"><a name="l00213"></a><span class="lineno"><a class="line" href="group__unittest.html#ga61e1bd261993b019232f5d5214423280">  213</a></span>&#160;<a class="code" href="group__unittest.html#ga0168f7b5f09ccbb226144053ad1b7497">TEST_CASE</a>( <span class="stringliteral">&quot;measureWithStats&quot;</span>, <span class="stringliteral">&quot;[gates]&quot;</span> ) {</div>
<div class="line"><a name="l00214"></a><span class="lineno">  214</span>&#160;    </div>
<div class="line"><a name="l00215"></a><span class="lineno">  215</span>&#160;    <a class="code" href="structQureg.html">Qureg</a> vec = <a class="code" href="group__type.html#ga3392816c0643414165c2f5caeec17df0">createQureg</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00216"></a><span class="lineno">  216</span>&#160;    <a class="code" href="structQureg.html">Qureg</a> mat = <a class="code" href="group__type.html#ga93e55b6650b408abb30a1d4a8bce757c">createDensityQureg</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00217"></a><span class="lineno">  217</span>&#160;        </div>
<div class="line"><a name="l00218"></a><span class="lineno">  218</span>&#160;    SECTION( <span class="stringliteral">&quot;correctness&quot;</span> ) {</div>
<div class="line"><a name="l00219"></a><span class="lineno">  219</span>&#160;        </div>
<div class="line"><a name="l00220"></a><span class="lineno">  220</span>&#160;        <span class="keywordtype">int</span> qubit = GENERATE( range(0,<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>) );</div>
<div class="line"><a name="l00221"></a><span class="lineno">  221</span>&#160;        </div>
<div class="line"><a name="l00222"></a><span class="lineno">  222</span>&#160;        <span class="comment">// repeat these random tests 10 times on every qubit </span></div>
<div class="line"><a name="l00223"></a><span class="lineno">  223</span>&#160;        GENERATE( range(0,10) );</div>
<div class="line"><a name="l00224"></a><span class="lineno">  224</span>&#160;        </div>
<div class="line"><a name="l00225"></a><span class="lineno">  225</span>&#160;        SECTION( <span class="stringliteral">&quot;state-vector&quot;</span> ) {</div>
<div class="line"><a name="l00226"></a><span class="lineno">  226</span>&#160;            </div>
<div class="line"><a name="l00227"></a><span class="lineno">  227</span>&#160;            <a class="code" href="group__testutilities.html#gac9263a65c96a458b810b20baa81c5b94">QVector</a> vecRef = <a class="code" href="group__testutilities.html#gae1d2eb70b50267f8186cbd53e300d94f">getRandomStateVector</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>);</div>
<div class="line"><a name="l00228"></a><span class="lineno">  228</span>&#160;            <a class="code" href="group__testutilities.html#gaf5a18d4c00dfe0523972be3bfc666362">toQureg</a>(vec, vecRef);</div>
<div class="line"><a name="l00229"></a><span class="lineno">  229</span>&#160;            </div>
<div class="line"><a name="l00230"></a><span class="lineno">  230</span>&#160;            <a class="code" href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a> res;</div>
<div class="line"><a name="l00231"></a><span class="lineno">  231</span>&#160;            <span class="keywordtype">int</span> outcome = <a class="code" href="group__normgate.html#gae192ac0759e46c7b20a7182020155240">measureWithStats</a>(vec, qubit, &amp;res);</div>
<div class="line"><a name="l00232"></a><span class="lineno">  232</span>&#160;            REQUIRE( (outcome == 0 || outcome == 1) );</div>
<div class="line"><a name="l00233"></a><span class="lineno">  233</span>&#160;            </div>
<div class="line"><a name="l00234"></a><span class="lineno">  234</span>&#160;            <span class="comment">// calculate prob of this outcome</span></div>
<div class="line"><a name="l00235"></a><span class="lineno">  235</span>&#160;            <a class="code" href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a> prob = 0;</div>
<div class="line"><a name="l00236"></a><span class="lineno">  236</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> ind=0; ind&lt;vecRef.size(); ind++) {</div>
<div class="line"><a name="l00237"></a><span class="lineno">  237</span>&#160;                <span class="keywordtype">int</span> bit = (ind &gt;&gt; qubit) &amp; 1; <span class="comment">// target-th bit</span></div>
<div class="line"><a name="l00238"></a><span class="lineno">  238</span>&#160;                <span class="keywordflow">if</span> (bit == outcome)</div>
<div class="line"><a name="l00239"></a><span class="lineno">  239</span>&#160;                    prob += pow(abs(vecRef[ind]), 2);</div>
<div class="line"><a name="l00240"></a><span class="lineno">  240</span>&#160;            }</div>
<div class="line"><a name="l00241"></a><span class="lineno">  241</span>&#160;            </div>
<div class="line"><a name="l00242"></a><span class="lineno">  242</span>&#160;            REQUIRE( prob == Approx(res) );</div>
<div class="line"><a name="l00243"></a><span class="lineno">  243</span>&#160;            </div>
<div class="line"><a name="l00244"></a><span class="lineno">  244</span>&#160;            <span class="comment">// renormalise by the outcome prob</span></div>
<div class="line"><a name="l00245"></a><span class="lineno">  245</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> ind=0; ind&lt;vecRef.size(); ind++) {</div>
<div class="line"><a name="l00246"></a><span class="lineno">  246</span>&#160;                <span class="keywordtype">int</span> bit = (ind &gt;&gt; qubit) &amp; 1; <span class="comment">// target-th bit</span></div>
<div class="line"><a name="l00247"></a><span class="lineno">  247</span>&#160;                <span class="keywordflow">if</span> (bit == outcome)</div>
<div class="line"><a name="l00248"></a><span class="lineno">  248</span>&#160;                    vecRef[ind] /= sqrt(prob);</div>
<div class="line"><a name="l00249"></a><span class="lineno">  249</span>&#160;                <span class="keywordflow">else</span> </div>
<div class="line"><a name="l00250"></a><span class="lineno">  250</span>&#160;                    vecRef[ind] = 0;</div>
<div class="line"><a name="l00251"></a><span class="lineno">  251</span>&#160;            }</div>
<div class="line"><a name="l00252"></a><span class="lineno">  252</span>&#160;            REQUIRE( <a class="code" href="group__testutilities.html#ga45cc399adad2e2a67b0124851189fae5">areEqual</a>(vec, vecRef) );</div>
<div class="line"><a name="l00253"></a><span class="lineno">  253</span>&#160;        }</div>
<div class="line"><a name="l00254"></a><span class="lineno">  254</span>&#160;        SECTION( <span class="stringliteral">&quot;density-matrix&quot;</span> ) {</div>
<div class="line"><a name="l00255"></a><span class="lineno">  255</span>&#160;            </div>
<div class="line"><a name="l00256"></a><span class="lineno">  256</span>&#160;            <a class="code" href="group__testutilities.html#gabbc686f5f40eff22e08a2b2012ac4e41">QMatrix</a> matRef = <a class="code" href="group__testutilities.html#ga617b0fd1eb1c395762c937ad3b23e61b">getRandomDensityMatrix</a>(<a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a>);</div>
<div class="line"><a name="l00257"></a><span class="lineno">  257</span>&#160;            <a class="code" href="group__testutilities.html#gaf5a18d4c00dfe0523972be3bfc666362">toQureg</a>(mat, matRef);</div>
<div class="line"><a name="l00258"></a><span class="lineno">  258</span>&#160;            </div>
<div class="line"><a name="l00259"></a><span class="lineno">  259</span>&#160;            <a class="code" href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a> res;</div>
<div class="line"><a name="l00260"></a><span class="lineno">  260</span>&#160;            <span class="keywordtype">int</span> outcome = <a class="code" href="group__normgate.html#gae192ac0759e46c7b20a7182020155240">measureWithStats</a>(mat, qubit, &amp;res);</div>
<div class="line"><a name="l00261"></a><span class="lineno">  261</span>&#160;            REQUIRE( (outcome == 0 || outcome == 1) );</div>
<div class="line"><a name="l00262"></a><span class="lineno">  262</span>&#160;            </div>
<div class="line"><a name="l00263"></a><span class="lineno">  263</span>&#160;            <span class="comment">// compute prob of this outcome</span></div>
<div class="line"><a name="l00264"></a><span class="lineno">  264</span>&#160;            <a class="code" href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a> prob = 0;</div>
<div class="line"><a name="l00265"></a><span class="lineno">  265</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> ind=0; ind&lt;matRef.size(); ind++) {</div>
<div class="line"><a name="l00266"></a><span class="lineno">  266</span>&#160;                <span class="keywordtype">int</span> bit = (ind &gt;&gt; qubit) &amp; 1; <span class="comment">// qubit-th bit</span></div>
<div class="line"><a name="l00267"></a><span class="lineno">  267</span>&#160;                <span class="keywordflow">if</span> (bit == outcome)</div>
<div class="line"><a name="l00268"></a><span class="lineno">  268</span>&#160;                    prob += real(matRef[ind][ind]);</div>
<div class="line"><a name="l00269"></a><span class="lineno">  269</span>&#160;            }</div>
<div class="line"><a name="l00270"></a><span class="lineno">  270</span>&#160;            </div>
<div class="line"><a name="l00271"></a><span class="lineno">  271</span>&#160;            REQUIRE( prob == Approx(res) );</div>
<div class="line"><a name="l00272"></a><span class="lineno">  272</span>&#160;                        </div>
<div class="line"><a name="l00273"></a><span class="lineno">  273</span>&#160;            <span class="comment">// renorm (/prob) every |*outcome*&gt;&lt;*outcome*| state, zeroing all others </span></div>
<div class="line"><a name="l00274"></a><span class="lineno">  274</span>&#160;            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> r=0; r&lt;matRef.size(); r++) {</div>
<div class="line"><a name="l00275"></a><span class="lineno">  275</span>&#160;                <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> c=0; c&lt;matRef.size(); c++) {</div>
<div class="line"><a name="l00276"></a><span class="lineno">  276</span>&#160;                    <span class="keywordtype">int</span> ketBit = (c &gt;&gt; qubit) &amp; 1;</div>
<div class="line"><a name="l00277"></a><span class="lineno">  277</span>&#160;                    <span class="keywordtype">int</span> braBit = (r &gt;&gt; qubit) &amp; 1;</div>
<div class="line"><a name="l00278"></a><span class="lineno">  278</span>&#160;                    </div>
<div class="line"><a name="l00279"></a><span class="lineno">  279</span>&#160;                    <span class="keywordflow">if</span> (ketBit == outcome &amp;&amp; braBit == outcome)</div>
<div class="line"><a name="l00280"></a><span class="lineno">  280</span>&#160;                        matRef[r][c] /= prob;</div>
<div class="line"><a name="l00281"></a><span class="lineno">  281</span>&#160;                    <span class="keywordflow">else</span></div>
<div class="line"><a name="l00282"></a><span class="lineno">  282</span>&#160;                        matRef[r][c] = 0;</div>
<div class="line"><a name="l00283"></a><span class="lineno">  283</span>&#160;                }</div>
<div class="line"><a name="l00284"></a><span class="lineno">  284</span>&#160;            }</div>
<div class="line"><a name="l00285"></a><span class="lineno">  285</span>&#160;            </div>
<div class="line"><a name="l00286"></a><span class="lineno">  286</span>&#160;            REQUIRE( <a class="code" href="group__testutilities.html#ga45cc399adad2e2a67b0124851189fae5">areEqual</a>(mat, matRef) );</div>
<div class="line"><a name="l00287"></a><span class="lineno">  287</span>&#160;        }</div>
<div class="line"><a name="l00288"></a><span class="lineno">  288</span>&#160;    }</div>
<div class="line"><a name="l00289"></a><span class="lineno">  289</span>&#160;    SECTION( <span class="stringliteral">&quot;input validation&quot;</span> ) {</div>
<div class="line"><a name="l00290"></a><span class="lineno">  290</span>&#160;        </div>
<div class="line"><a name="l00291"></a><span class="lineno">  291</span>&#160;        SECTION( <span class="stringliteral">&quot;qubit index&quot;</span> ) {</div>
<div class="line"><a name="l00292"></a><span class="lineno">  292</span>&#160;            </div>
<div class="line"><a name="l00293"></a><span class="lineno">  293</span>&#160;            <span class="keywordtype">int</span> qubit = GENERATE( -1, <a class="code" href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a> );</div>
<div class="line"><a name="l00294"></a><span class="lineno">  294</span>&#160;            <a class="code" href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a> res;</div>
<div class="line"><a name="l00295"></a><span class="lineno">  295</span>&#160;            REQUIRE_THROWS_WITH( <a class="code" href="group__normgate.html#gae192ac0759e46c7b20a7182020155240">measureWithStats</a>(vec, qubit, &amp;res), Contains(<span class="stringliteral">&quot;Invalid target qubit&quot;</span>) );</div>
<div class="line"><a name="l00296"></a><span class="lineno">  296</span>&#160;        }</div>
<div class="line"><a name="l00297"></a><span class="lineno">  297</span>&#160;    }</div>
<div class="line"><a name="l00298"></a><span class="lineno">  298</span>&#160;    <a class="code" href="group__type.html#ga8e1e6ad3254892edc2ee08712ea9d692">destroyQureg</a>(vec, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00299"></a><span class="lineno">  299</span>&#160;    <a class="code" href="group__type.html#ga8e1e6ad3254892edc2ee08712ea9d692">destroyQureg</a>(mat, <a class="code" href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a>);</div>
<div class="line"><a name="l00300"></a><span class="lineno">  300</span>&#160;}</div>
</div><!-- fragment --></div><!-- contents -->
<div class="ttc" id="amain_8cpp_html_a718130cc1fc36cde991aff3d4c85c47d"><div class="ttname"><a href="main_8cpp.html#a718130cc1fc36cde991aff3d4c85c47d">QUEST_ENV</a></div><div class="ttdeci">QuESTEnv QUEST_ENV</div><div class="ttdoc">The global QuESTEnv instance, to be created and destroyed once in this main(), so that the MPI enviro...</div><div class="ttdef"><b>Definition:</b> <a href="main_8cpp_source.html#l00020">main.cpp:20</a></div></div>
<div class="ttc" id="autilities_8hpp_html_a2f52338126bb8cdf7fcf68eaf01c546a"><div class="ttname"><a href="utilities_8hpp.html#a2f52338126bb8cdf7fcf68eaf01c546a">NUM_QUBITS</a></div><div class="ttdeci">#define NUM_QUBITS</div><div class="ttdoc">The default number of qubits in the registers created for unit testing (both statevectors and density...</div><div class="ttdef"><b>Definition:</b> <a href="utilities_8hpp_source.html#l00036">utilities.hpp:36</a></div></div>
<div class="ttc" id="agroup__normgate_html_ga34457f7f62ccd75fb7d3fe67cd2f815a"><div class="ttname"><a href="group__normgate.html#ga34457f7f62ccd75fb7d3fe67cd2f815a">collapseToOutcome</a></div><div class="ttdeci">qreal collapseToOutcome(Qureg qureg, int measureQubit, int outcome)</div><div class="ttdoc">Updates qureg to be consistent with measuring measureQubit in the given outcome (0 or 1),...</div><div class="ttdef"><b>Definition:</b> <a href="QuEST_8c_source.html#l00726">QuEST.c:726</a></div></div>
<div class="ttc" id="aQuEST_8h_html"><div class="ttname"><a href="QuEST_8h.html">QuEST.h</a></div></div>
<div class="ttc" id="agroup__normgate_html_ga2a3794103125f1e3cfa103f8b1963656"><div class="ttname"><a href="group__normgate.html#ga2a3794103125f1e3cfa103f8b1963656">measure</a></div><div class="ttdeci">int measure(Qureg qureg, int measureQubit)</div><div class="ttdoc">Measures a single qubit, collapsing it randomly to 0 or 1.</div><div class="ttdef"><b>Definition:</b> <a href="QuEST_8c_source.html#l00758">QuEST.c:758</a></div></div>
<div class="ttc" id="agroup__type_html_ga7740e349b4f8bae6451547680f0ce2d6"><div class="ttname"><a href="group__type.html#ga7740e349b4f8bae6451547680f0ce2d6">qreal</a></div><div class="ttdeci">#define qreal</div></div>
<div class="ttc" id="agroup__testutilities_html_gaf5a18d4c00dfe0523972be3bfc666362"><div class="ttname"><a href="group__testutilities.html#gaf5a18d4c00dfe0523972be3bfc666362">toQureg</a></div><div class="ttdeci">void toQureg(Qureg qureg, QVector vec)</div><div class="ttdoc">Initialises the state-vector qureg to have the same amplitudes as vec.</div><div class="ttdef"><b>Definition:</b> <a href="utilities_8cpp_source.html#l01026">utilities.cpp:1026</a></div></div>
<div class="ttc" id="agroup__normgate_html_gae192ac0759e46c7b20a7182020155240"><div class="ttname"><a href="group__normgate.html#gae192ac0759e46c7b20a7182020155240">measureWithStats</a></div><div class="ttdeci">int measureWithStats(Qureg qureg, int measureQubit, qreal *outcomeProb)</div><div class="ttdoc">Measures a single qubit, collapsing it randomly to 0 or 1, and additionally gives the probability of ...</div><div class="ttdef"><b>Definition:</b> <a href="QuEST_8c_source.html#l00745">QuEST.c:745</a></div></div>
<div class="ttc" id="agroup__testutilities_html_gac9263a65c96a458b810b20baa81c5b94"><div class="ttname"><a href="group__testutilities.html#gac9263a65c96a458b810b20baa81c5b94">QVector</a></div><div class="ttdeci">std::vector&lt; qcomp &gt; QVector</div><div class="ttdoc">A complex vector, which can be zero-initialised with QVector(numAmps).</div><div class="ttdef"><b>Definition:</b> <a href="utilities_8hpp_source.html#l00060">utilities.hpp:60</a></div></div>
<div class="ttc" id="agroup__type_html_gad863c5878a01051a2726ce94bf365428"><div class="ttname"><a href="group__type.html#gad863c5878a01051a2726ce94bf365428">qcomp</a></div><div class="ttdeci">#define qcomp</div></div>
<div class="ttc" id="agroup__type_html_ga8e1e6ad3254892edc2ee08712ea9d692"><div class="ttname"><a href="group__type.html#ga8e1e6ad3254892edc2ee08712ea9d692">destroyQureg</a></div><div class="ttdeci">void destroyQureg(Qureg qureg, QuESTEnv env)</div><div class="ttdoc">Deallocate a Qureg object representing a set of qubits.</div><div class="ttdef"><b>Definition:</b> <a href="QuEST_8c_source.html#l00077">QuEST.c:77</a></div></div>
<div class="ttc" id="agroup__testutilities_html_gae1d2eb70b50267f8186cbd53e300d94f"><div class="ttname"><a href="group__testutilities.html#gae1d2eb70b50267f8186cbd53e300d94f">getRandomStateVector</a></div><div class="ttdeci">QVector getRandomStateVector(int numQb)</div><div class="ttdoc">Returns a random numQb-length L2-normalised state-vector from an undisclosed distribution.</div><div class="ttdef"><b>Definition:</b> <a href="utilities_8cpp_source.html#l00453">utilities.cpp:453</a></div></div>
<div class="ttc" id="agroup__init_html_ga0379e21efca0767a22607fc0e26a5a22"><div class="ttname"><a href="group__init.html#ga0379e21efca0767a22607fc0e26a5a22">initClassicalState</a></div><div class="ttdeci">void initClassicalState(Qureg qureg, long long int stateInd)</div><div class="ttdoc">Initialise a set of  qubits to the classical state (also known as a &quot;computational basis state&quot;) with...</div><div class="ttdef"><b>Definition:</b> <a href="QuEST_8c_source.html#l00134">QuEST.c:134</a></div></div>
<div class="ttc" id="astructQureg_html"><div class="ttname"><a href="structQureg.html">Qureg</a></div><div class="ttdoc">Represents a system of qubits.</div><div class="ttdef"><b>Definition:</b> <a href="QuEST_8h_source.html#l00203">QuEST.h:203</a></div></div>
<div class="ttc" id="agroup__testutilities_html_gabbc686f5f40eff22e08a2b2012ac4e41"><div class="ttname"><a href="group__testutilities.html#gabbc686f5f40eff22e08a2b2012ac4e41">QMatrix</a></div><div class="ttdeci">std::vector&lt; std::vector&lt; qcomp &gt; &gt; QMatrix</div><div class="ttdoc">A complex square matrix.</div><div class="ttdef"><b>Definition:</b> <a href="utilities_8hpp_source.html#l00049">utilities.hpp:49</a></div></div>
<div class="ttc" id="agroup__type_html_ga3392816c0643414165c2f5caeec17df0"><div class="ttname"><a href="group__type.html#ga3392816c0643414165c2f5caeec17df0">createQureg</a></div><div class="ttdeci">Qureg createQureg(int numQubits, QuESTEnv env)</div><div class="ttdoc">Create a Qureg object representing a set of qubits which will remain in a pure state.</div><div class="ttdef"><b>Definition:</b> <a href="QuEST_8c_source.html#l00036">QuEST.c:36</a></div></div>
<div class="ttc" id="agroup__testutilities_html_ga617b0fd1eb1c395762c937ad3b23e61b"><div class="ttname"><a href="group__testutilities.html#ga617b0fd1eb1c395762c937ad3b23e61b">getRandomDensityMatrix</a></div><div class="ttdeci">QMatrix getRandomDensityMatrix(int numQb)</div><div class="ttdoc">Returns a random numQb-by-numQb density matrix, from an undisclosed distribution, in a very mixed sta...</div><div class="ttdef"><b>Definition:</b> <a href="utilities_8cpp_source.html#l00457">utilities.cpp:457</a></div></div>
<div class="ttc" id="agroup__unittest_html_ga0168f7b5f09ccbb226144053ad1b7497"><div class="ttname"><a href="group__unittest.html#ga0168f7b5f09ccbb226144053ad1b7497">TEST_CASE</a></div><div class="ttdeci">TEST_CASE(&quot;collapseToOutcome&quot;, &quot;[gates]&quot;)</div><div class="ttdef"><b>Definition:</b> <a href="test__gates_8cpp_source.html#l00015">test_gates.cpp:15</a></div></div>
<div class="ttc" id="agroup__init_html_ga2f049055d4a51d0ea4d1a43b5234dedb"><div class="ttname"><a href="group__init.html#ga2f049055d4a51d0ea4d1a43b5234dedb">initZeroState</a></div><div class="ttdeci">void initZeroState(Qureg qureg)</div><div class="ttdoc">Initialise a set of  qubits to the classical zero state .</div><div class="ttdef"><b>Definition:</b> <a href="QuEST_8c_source.html#l00113">QuEST.c:113</a></div></div>
<div class="ttc" id="agroup__testutilities_html_ga45cc399adad2e2a67b0124851189fae5"><div class="ttname"><a href="group__testutilities.html#ga45cc399adad2e2a67b0124851189fae5">areEqual</a></div><div class="ttdeci">bool areEqual(QVector a, QVector b)</div><div class="ttdoc">Returns true if the absolute value of the difference between every amplitude in vectors a and b is le...</div><div class="ttdef"><b>Definition:</b> <a href="utilities_8cpp_source.html#l00387">utilities.cpp:387</a></div></div>
<div class="ttc" id="agroup__type_html_ga93e55b6650b408abb30a1d4a8bce757c"><div class="ttname"><a href="group__type.html#ga93e55b6650b408abb30a1d4a8bce757c">createDensityQureg</a></div><div class="ttdeci">Qureg createDensityQureg(int numQubits, QuESTEnv env)</div><div class="ttdoc">Create a Qureg for qubits which are represented by a density matrix, and can be in mixed states.</div><div class="ttdef"><b>Definition:</b> <a href="QuEST_8c_source.html#l00050">QuEST.c:50</a></div></div>
<div class="ttc" id="autilities_8hpp_html"><div class="ttname"><a href="utilities_8hpp.html">utilities.hpp</a></div></div>
<!-- HTML footer for doxygen 1.8.8-->
<!-- start footer part -->
</div>
</div>
</div>
</div>
</div>
</body>
        <script type="text/javascript" src="boot.js"></script>
</html>
